package cn.nextProject.controller;

import cn.nextProject.entity.Result;
import cn.nextProject.entity.UserRoleGroup;
import cn.nextProject.service.UserRoleGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Slf4j
@RestController
public class UserRoleGroupController {

    @Autowired
    private UserRoleGroupService userRoleGroupService;

    @GetMapping("/findOneUserRoleGroupDetails")
    public Result<?> findOneUserRoleGroupDetails(@RequestParam("roleGroupId") Long roleGroupId,
                                                 @RequestParam("username") String username,
                                                 @RequestParam("realname") String realname) {

        if (ObjectUtils.isEmpty(roleGroupId)){
            return new Result<>().fail("角色组ID不能为空");
        }

        UserRoleGroup userRoleGroup = userRoleGroupService.findOne(roleGroupId,username,realname);
        return new Result<>().success("查询成功",userRoleGroup);
    }

    @Transactional
    @PostMapping("/saveUserRoleGroup")
    public Result<?> saveUserRoleGroup(@RequestBody UserRoleGroup userRoleGroup) {

        if (ObjectUtils.isEmpty(userRoleGroup)){
            return new Result<>().fail("用户角色组不能为空");
        }

        if (ObjectUtils.isEmpty(userRoleGroup.getRoleGroupId())){
            return new Result<>().fail("角色组ID不能为空");
        }
        if (CollectionUtils.isEmpty(userRoleGroup.getUsernames()) || userRoleGroup.getUsernames().size() == 0){
            return new Result<>().fail("用户名不能为空");
        }

        List<UserRoleGroup> userRoleGroupList = new ArrayList<>();
        StringBuffer stringBuffer = new StringBuffer();
        for (String username : userRoleGroup.getUsernames()) {
            if (ObjectUtils.isEmpty(username)){
                log.info("用户名不能为空:{}", username);
                stringBuffer.append("用户名不能为空: "+ username);
                continue;
            }
            UserRoleGroup userRoleGroup1 = userRoleGroupService.findOne(userRoleGroup.getRoleGroupId(), username, null);
            if (!ObjectUtils.isEmpty(userRoleGroup1)){
                log.info("角色组已存在此用户:{}",username);
                stringBuffer.append("角色组已存在此用户: "+ username);
                continue;
            }
            UserRoleGroup userRoleGroup2 = new UserRoleGroup();
            userRoleGroup2.setRoleGroupId(userRoleGroup.getRoleGroupId());
            userRoleGroup2.setUsername(username);
            userRoleGroupList.add(userRoleGroup2);
            if (userRoleGroupList.size() == 1000){
                userRoleGroupService.saveBatch(userRoleGroupList);
                userRoleGroupList.clear();
            }
        }

        if (userRoleGroupList.size() > 0){
            userRoleGroupService.saveBatch(userRoleGroupList);
        }

        return new Result<>().success("保存成功",stringBuffer.toString());
    }

    @GetMapping("/deleteUserRoleGroup")
    public Result<?> deleteUserRoleGroup(@RequestParam("id") Long id) {

        userRoleGroupService.delete(id);

        return new Result<>().success("删除成功",null);

    }

}
